package com.jinzhi.jzweb.service.impl.moduleConfig;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.CoreServiceImpl;
import com.jinzhi.common.type.EnumErrorCode;
import com.jinzhi.common.utils.PasswdUtils;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.dao.DecryptDao;
import com.jinzhi.jzweb.dao.ExaminationResultsComDao;
import com.jinzhi.jzweb.domain.ExaminationResultsComDO;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.domain.moduleConfig.CareStuSignUpComRealityDO;
import com.jinzhi.jzweb.domain.moduleConfig.DecryptDO;
import com.jinzhi.jzweb.domain.moduleConfig.EncipherDO;
import com.jinzhi.jzweb.pojo.moduleConfig.DecryptDO1;
import com.jinzhi.jzweb.pojo.moduleConfig.DecryptNum1VO;
import com.jinzhi.jzweb.pojo.moduleConfig.DecryptNum2VO;
import com.jinzhi.jzweb.pojo.moduleConfig.DecryptNumVO;
import com.jinzhi.jzweb.service.ExaminationResultsComService;
import com.jinzhi.jzweb.service.StuSignUpComService;
import com.jinzhi.jzweb.service.moduleConfig.CareStuSignUpComRealityService;
import com.jinzhi.jzweb.service.moduleConfig.DecryptService;
import com.jinzhi.jzweb.service.moduleConfig.EncipherService;
import com.jinzhi.jzweb.utils.CustomerDailyImportListener;
import com.jinzhi.jzweb.utils.CustomerDailyImportListener2;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.jzweb.utils.StringConverter;
import com.jinzhi.sys.dao.UserDao;
import com.jinzhi.sys.domain.RoleDO;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.*;


/*
 *  @author : JingChangTian
 *  @date   : 2024/3/18 20:20
 *  @description :  解密
 *
 */
@Service
public class DecryptServiceImpl extends CoreServiceImpl<DecryptDao, DecryptDO> implements DecryptService {


    private final CareStuSignUpComRealityService comRealityService;

    private final ExaminationResultsComService examinationResultsComService;

    private final EncipherService encipherService;

    private final CareStuSignUpComRealityService careStuSignUpComRealityService;

    private final RoleService roleService;

    private final StuSignUpComService stuSignUpComService;

    private final UserDao userDao;
    private final ExaminationResultsComDao examinationResultsDao;

    private final String refereeName = "裁判长";
    private final String arbitrationName = "仲裁长";

    public DecryptServiceImpl(CareStuSignUpComRealityService comRealityService, ExaminationResultsComService examinationResultsComService, EncipherService encipherService, CareStuSignUpComRealityService careStuSignUpComRealityService, RoleService roleService,
                              StuSignUpComService stuSignUpComService, UserDao userDao,ExaminationResultsComDao examinationResultsDao) {
        this.comRealityService = comRealityService;
        this.examinationResultsComService = examinationResultsComService;
        this.encipherService = encipherService;
        this.careStuSignUpComRealityService = careStuSignUpComRealityService;
        this.roleService = roleService;
        this.stuSignUpComService = stuSignUpComService;
        this.userDao = userDao;
        this.examinationResultsDao = examinationResultsDao;
    }

    @Override
    public Result importExcelDecryptNum1(MultipartFile file, String field) {
        List<DecryptDO1> lottery = new ArrayList<>();
        if (!"application/vnd.ms-excel".equals(file.getContentType())) {
            return Result.fail("文件格式错误，请选择.xls文件");
        }

        try {
            InputStream inputStream = file.getInputStream();
            lottery = EasyExcel.read(inputStream)
                    // 这个转换是成全局的， 所有java为string,excel为string的都会用这个转换器。
                    // 如果就想单个字段使用请使用@ExcelProperty 指定converter
                    .registerConverter(new StringConverter())
                    // 注册监听器，可以在这里校验字段
                    .registerReadListener(new CustomerDailyImportListener())
                    .head(DecryptDO1.class)
                    .sheet()
                    .headRowNumber(2)
                    .doReadSync();
        } catch (Exception e) {
            e.getMessage();
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        if (lottery.isEmpty()) {
            return Result.fail("Excel数据为空，请检查");
        }
        //根据赛场查询所有的一次加密
        List<DecryptDO> decryptDOS = this.selectList(new EntityWrapper<DecryptDO>().eq("field", field));
        List<DecryptDO> decryptDOS1 = this.selectList(new EntityWrapper<DecryptDO>().eq("field", field).andNew("num1 = {0} or num1 is null", "-"));
        if (decryptDOS1.isEmpty()) {
            return Result.fail("没有二次加密信息");
        }
        //验证一次加密信息
        for (int i = 0; i < lottery.size(); i++) {
            for (int j = 0; j < decryptDOS.size(); j++) {
                if (Objects.equals(lottery.get(i).getNum1(), decryptDOS.get(j).getNum1())) {
                    return Result.fail("一次加密" + lottery.get(i).getNum1() + "已存在");
                }
            }
        }

        //理论
        //优先修改"-"的二次加密，当"-"不存在了，则新增
//        List<DecryptDO> insertList = new ArrayList<>();
//        for (int i = 0; i < lottery.size(); i++) {
//            // 判断 导入的一次机密号小于等于二次加密号
//            if (decryptDOS1.size() >= i + 1) {
//                decryptDOS1.get(i).setNum1(lottery.get(i).getNum1());
//            }
//            //新增  导入的一次机密号大于二次加密号
//            else if (decryptDOS1.size() < i + 1) {
//                DecryptDO decryptDO = new DecryptDO();
//                BeanUtils.copyProperties(lottery.get(i), decryptDO);
//                decryptDO.setField(field);
//                insertList.add(decryptDO);
//            }
//
//        }
//        if (null != decryptDOS1 && !decryptDOS1.isEmpty()) {
//            this.updateBatchById(decryptDOS1);
//        }
//        if (null != insertList && !insertList.isEmpty()) {
//            this.insertBatch(insertList);
//        }
//        return Result.ok();

        //赋予一次加密
        for (int i = 0; i < decryptDOS1.size(); i++) {
            if (lottery.size() <= i) {
                break;
            }
            decryptDOS1.get(i).setNum1(lottery.get(i).getNum1());
        }

        return Result.ok(this.updateBatchById(decryptDOS1));
    }

    @Override
    public Result importExcelDecryptNum2(MultipartFile file, String field) {
        List<DecryptDO> lottery = new ArrayList<>();
        if (!"application/vnd.ms-excel".equals(file.getContentType())) {
            return Result.fail("文件格式错误，请选择.xls文件");
        }
        try {
            InputStream inputStream = file.getInputStream();
            lottery = EasyExcel.read(inputStream)
                    // 这个转换是成全局的， 所有java为string,excel为string的都会用这个转换器。
                    // 如果就想单个字段使用请使用@ExcelProperty 指定converter
                    .registerConverter(new StringConverter())
                    // 注册监听器，可以在这里校验字段
                    .registerReadListener(new CustomerDailyImportListener2())
                    .head(DecryptDO.class)
                    .sheet()
                    .headRowNumber(2)
                    .doReadSync();
        } catch (Exception e) {
            e.getMessage();
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        if (lottery.isEmpty()) {
            return Result.fail("Excel数据为空，请检查");
        }
        lottery.forEach(a -> {
            a.setField(field);
        });
        //根据赛场查询所有的一次加密
        List<DecryptDO> decryptDOS = this.selectList(new EntityWrapper<DecryptDO>().eq("field", field));
        List<DecryptDO> decryptDOS1 = this.selectList(new EntityWrapper<DecryptDO>().eq("field",field).andNew("num2 = {0} or num2 is null","-"));
        if (decryptDOS.isEmpty()) {
            return Result.ok(this.insertBatch(lottery));
        }
        //验证二次加密信息
        for (int i = 0; i < lottery.size(); i++) {
            for (int j = 0; j < decryptDOS.size(); j++) {
                if (Objects.equals(lottery.get(i).getNum2(), decryptDOS.get(j).getNum2())) {
                    return Result.fail("二次加密" + lottery.get(i).getNum2() + "已存在");
                }
            }
        }
        //赋予一次加密
        //优先修改"-"的二次加密，当"-"不存在了，则新增
        if(decryptDOS==null || decryptDOS.isEmpty()){
            return Result.ok(this.insertBatch(lottery));
        }
        List<DecryptDO> insertList = new ArrayList<>();
        for (int i = 0; i < lottery.size(); i++) {
            if(decryptDOS1.size()>=i+1){
                decryptDOS1.get(i).setNum2(lottery.get(i).getNum2());
            }
            //新增
            else if (decryptDOS1.size()<i+1) {
                insertList.add(lottery.get(i));
            }

        }
        this.updateBatchById(decryptDOS1);
        return Result.ok(this.insertBatch(insertList));
    }


    @Override
    @Transactional
    public Result importExcelDecryptShiCaoNum1(MultipartFile file) {
        List<DecryptDO1> lottery = new ArrayList<>();
        if (!"application/vnd.ms-excel".equals(file.getContentType())) {
            return Result.fail("文件格式错误，请选择.xls文件");
        }

        try {
            InputStream inputStream = file.getInputStream();
            lottery = EasyExcel.read(inputStream)
                    // 这个转换是成全局的， 所有java为string,excel为string的都会用这个转换器。
                    // 如果就想单个字段使用请使用@ExcelProperty 指定converter
                    .registerConverter(new StringConverter())
                    // 注册监听器，可以在这里校验字段
                    .registerReadListener(new CustomerDailyImportListener())
                    .head(DecryptDO1.class)
                    .sheet()
                    .headRowNumber(2)
                    .doReadSync();
        } catch (Exception e) {
            e.getMessage();
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        if (lottery.isEmpty()) {
            return Result.fail("Excel数据为空，请检查");
        }

        //根据赛场查询所有的一次加密
        List<DecryptDO> decryptDOS = this.selectList(new EntityWrapper<DecryptDO>().eq("field", "B1"));
        List<DecryptDO> decryptDOS1 = this.selectList(new EntityWrapper<DecryptDO>().eq("field", "B1").andNew("num1 = {0} or num1 is null", "-"));
        List<DecryptDO> decryptDOS2 = this.selectList(new EntityWrapper<DecryptDO>().eq("field", "B2"));
        List<DecryptDO> decryptDOS22 = this.selectList(new EntityWrapper<DecryptDO>().eq("field", "B2").andNew("num1 = {0} or num1 is null", "-"));
        List<DecryptDO> decryptDOS3 = this.selectList(new EntityWrapper<DecryptDO>().eq("field", "B3"));
        List<DecryptDO> decryptDOS33 = this.selectList(new EntityWrapper<DecryptDO>().eq("field", "B3").andNew("num1 = {0} or num1 is null", "-"));
        List<DecryptDO> decryptDOS4 = this.selectList(new EntityWrapper<DecryptDO>().eq("field", "B4"));
        List<DecryptDO> decryptDOS44 = this.selectList(new EntityWrapper<DecryptDO>().eq("field", "B4").andNew("num1 = {0} or num1 is null", "-"));
        if (decryptDOS1.isEmpty()) {
            return Result.fail("没有二次加密信息");
        }
        if (decryptDOS22.isEmpty()) {
            return Result.fail("没有二次加密信息");
        }
        if (decryptDOS33.isEmpty()) {
            return Result.fail("没有二次加密信息");
        }
        if (decryptDOS44.isEmpty()) {
            return Result.fail("没有二次加密信息");
        }
        //验证一次加密信息
        for (int i = 0; i < lottery.size(); i++) {
            for (int j = 0; j < decryptDOS.size(); j++) {
                if (Objects.equals(lottery.get(i).getNum1(), decryptDOS.get(j).getNum1())) {
                    return Result.fail("一次加密" + lottery.get(i).getNum1() + "已存在");
                }
            }
        }
        for (int i = 0; i < lottery.size(); i++) {
            for (int j = 0; j < decryptDOS2.size(); j++) {
                if (Objects.equals(lottery.get(i).getNum1(), decryptDOS2.get(j).getNum1())) {
                    return Result.fail("一次加密" + lottery.get(i).getNum1() + "已存在");
                }
            }
        }
        for (int i = 0; i < lottery.size(); i++) {
            for (int j = 0; j < decryptDOS3.size(); j++) {
                if (Objects.equals(lottery.get(i).getNum1(), decryptDOS3.get(j).getNum1())) {
                    return Result.fail("一次加密" + lottery.get(i).getNum1() + "已存在");
                }
            }
        }
        for (int i = 0; i < lottery.size(); i++) {
            for (int j = 0; j < decryptDOS4.size(); j++) {
                if (Objects.equals(lottery.get(i).getNum1(), decryptDOS4.get(j).getNum1())) {
                    return Result.fail("一次加密" + lottery.get(i).getNum1() + "已存在");
                }
            }
        }
        //赋予一次加密
        for (int i = 0; i < decryptDOS1.size(); i++) {
            if (lottery.size() <= i) {
                break;
            }
            decryptDOS1.get(i).setNum1(lottery.get(i).getNum1());
        }
        for (int i = 0; i < decryptDOS22.size(); i++) {
            if (lottery.size() <= i) {
                break;
            }
            decryptDOS22.get(i).setNum1(lottery.get(i).getNum1());
        }
        for (int i = 0; i < decryptDOS33.size(); i++) {
            if (lottery.size() <= i) {
                break;
            }
            decryptDOS33.get(i).setNum1(lottery.get(i).getNum1());
        }
        for (int i = 0; i < decryptDOS44.size(); i++) {
            if (lottery.size() <= i) {
                break;
            }
            decryptDOS44.get(i).setNum1(lottery.get(i).getNum1());
        }

        // 更新
        this.updateBatchById(decryptDOS1);
        this.updateBatchById(decryptDOS22);
        this.updateBatchById(decryptDOS33);
        this.updateBatchById(decryptDOS44);


        //实操

        //优先修改"-"的二次加密，当"-"不存在了，则新增
//        List<DecryptDO> insertList = new ArrayList<>();
//        for (int i = 0; i < lottery.size(); i++) {
//            // 判断导入的一次加密号小于等于二次加密号情况
//            if (decryptDOS1.size() >= i + 1) {
//                decryptDOS1.get(i).setNum1(lottery.get(i).getNum1());
//            }
//            if (decryptDOS22.size() >= i + 1) {
//                decryptDOS22.get(i).setNum1(lottery.get(i).getNum1());
//            }
//            if (decryptDOS33.size() >= i + 1) {
//                decryptDOS33.get(i).setNum1(lottery.get(i).getNum1());
//            }
//            if (decryptDOS44.size() >= i + 1) {
//                decryptDOS44.get(i).setNum1(lottery.get(i).getNum1());
//            }
//            //新增 判断导入的一次加密号大于二次加密号情况
//            if (decryptDOS1.size() < i + 1) {
//                DecryptDO decryptDO1 = new DecryptDO();
//                BeanUtils.copyProperties(lottery.get(i), decryptDO1);
//                decryptDO1.setField("B1");
//                insertList.add(decryptDO1);
//            }
//            if (decryptDOS22.size() < i + 1) {
//                DecryptDO decryptDO2 = new DecryptDO();
//                BeanUtils.copyProperties(lottery.get(i), decryptDO2);
//                decryptDO2.setField("B2");
//                insertList.add(decryptDO2);
//            }
//            if (decryptDOS33.size() < i + 1) {
//                DecryptDO decryptDO3 = new DecryptDO();
//                BeanUtils.copyProperties(lottery.get(i), decryptDO3);
//                decryptDO3.setField("B3");
//                insertList.add(decryptDO3);
//            }
//            if (decryptDOS44.size() < i + 1) {
//                DecryptDO decryptDO4 = new DecryptDO();
//                BeanUtils.copyProperties(lottery.get(i), decryptDO4);
//                decryptDO4.setField("B4");
//                insertList.add(decryptDO4);
//            }
//
//        }
//        // 新增
//        if (null != decryptDOS1 && !decryptDOS1.isEmpty()) {
//            this.updateBatchById(decryptDOS1);
//        }
//        if (null != decryptDOS22 && !decryptDOS22.isEmpty()) {
//            this.updateBatchById(decryptDOS22);
//        }
//        if (null != decryptDOS33 && !decryptDOS33.isEmpty()) {
//            this.updateBatchById(decryptDOS33);
//        }
//        if (null != decryptDOS44 && !decryptDOS44.isEmpty()) {
//            this.updateBatchById(decryptDOS44);
//        }
//        if (null != insertList && !insertList.isEmpty()) {
//            this.insertBatch(insertList);
//        }

        return Result.ok();
    }

    @Override
    @Transactional
    public Result importExcelDecryptShiCaoNum2(MultipartFile file) {
        List<DecryptDO> lottery = new ArrayList<>();
        List<DecryptDO> lottery1 = new ArrayList<>();
        List<DecryptDO> lottery2 = new ArrayList<>();
        List<DecryptDO> lottery3 = new ArrayList<>();
        List<DecryptDO> lottery4 = new ArrayList<>();
        if (!"application/vnd.ms-excel".equals(file.getContentType())) {
            return Result.fail("文件格式错误，请选择.xls文件");
        }
        try {
            InputStream inputStream = file.getInputStream();
            lottery = EasyExcel.read(inputStream)
                    // 这个转换是成全局的， 所有java为string,excel为string的都会用这个转换器。
                    // 如果就想单个字段使用请使用@ExcelProperty 指定converter
                    .registerConverter(new StringConverter())
                    // 注册监听器，可以在这里校验字段
                    .registerReadListener(new CustomerDailyImportListener2())
                    .head(DecryptDO.class)
                    .sheet()
                    .headRowNumber(2)
                    .doReadSync();
        } catch (Exception e) {
            e.getMessage();
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        if (lottery.isEmpty()) {
            return Result.fail("Excel数据为空，请检查");
        }
        // 循环赋值，设置实操B1,B2,B3,B4
        for (DecryptDO decryptDO : lottery) {
            DecryptDO copy1 = new DecryptDO();
            DecryptDO copy2 = new DecryptDO();
            DecryptDO copy3 = new DecryptDO();
            DecryptDO copy4 = new DecryptDO();

            BeanUtils.copyProperties(decryptDO, copy1);
            BeanUtils.copyProperties(decryptDO, copy2);
            BeanUtils.copyProperties(decryptDO, copy3);
            BeanUtils.copyProperties(decryptDO, copy4);

            copy1.setField("B1");
            copy2.setField("B2");
            copy3.setField("B3");
            copy4.setField("B4");

            lottery1.add(copy1);
            lottery2.add(copy2);
            lottery3.add(copy3);
            lottery4.add(copy4);
        }

        //根据赛场查询所有的一次加密
        List<DecryptDO> decryptDOS1 = this.selectList(new EntityWrapper<DecryptDO>().eq("field", "B1"));
        List<DecryptDO> decryptDOS2 = this.selectList(new EntityWrapper<DecryptDO>().eq("field", "B2"));
        List<DecryptDO> decryptDOS3 = this.selectList(new EntityWrapper<DecryptDO>().eq("field", "B3"));
        List<DecryptDO> decryptDOS4 = this.selectList(new EntityWrapper<DecryptDO>().eq("field", "B4"));
        if (decryptDOS1.isEmpty()) {
            this.insertBatch(lottery1);
            //return Result.ok(this.insertBatch(lottery1));
        }
        if (decryptDOS2.isEmpty()) {
            this.insertBatch(lottery2);
            //return Result.ok(this.insertBatch(lottery2));
        }
        if (decryptDOS3.isEmpty()) {
            this.insertBatch(lottery3);
            //return Result.ok(this.insertBatch(lottery3));
        }
        if (decryptDOS4.isEmpty()) {
            this.insertBatch(lottery4);
            //return Result.ok(this.insertBatch(lottery));
        }
        //验证二次加密信息
        for (int i = 0; i < lottery1.size(); i++) {
            for (int j = 0; j < decryptDOS1.size(); j++) {
                if (Objects.equals(lottery1.get(i).getNum2(), decryptDOS1.get(j).getNum2())) {
                    return Result.fail("一次加密" + lottery1.get(i).getNum2() + "已存在");
                }
            }
        }
        for (int i = 0; i < lottery2.size(); i++) {
            for (int j = 0; j < decryptDOS2.size(); j++) {
                if (Objects.equals(lottery2.get(i).getNum2(), decryptDOS2.get(j).getNum2())) {
                    return Result.fail("一次加密" + lottery2.get(i).getNum2() + "已存在");
                }
            }
        }
        for (int i = 0; i < lottery3.size(); i++) {
            for (int j = 0; j < decryptDOS3.size(); j++) {
                if (Objects.equals(lottery3.get(i).getNum2(), decryptDOS3.get(j).getNum2())) {
                    return Result.fail("一次加密" + lottery3.get(i).getNum2() + "已存在");
                }
            }
        }
        for (int i = 0; i < lottery4.size(); i++) {
            for (int j = 0; j < decryptDOS4.size(); j++) {
                if (Objects.equals(lottery4.get(i).getNum2(), decryptDOS4.get(j).getNum2())) {
                    return Result.fail("一次加密" + lottery4.get(i).getNum2() + "已存在");
                }
            }
        }
        //return Result.ok(this.insertBatch(lottery));
        return Result.ok();
    }

    @Override
    @Transactional
    public Result updateDecrypt(DecryptDO decryptDO) {
        DecryptDO decryptDO1 = this.selectById(decryptDO.getId());
        //验证一二加密是否已经存在
        List<DecryptDO> doList =  this.selectList(new EntityWrapper<DecryptDO>()
                .eq("field",decryptDO.getField())
                .eq(StringUtil.notEmpty(decryptDO.getNum2()),"num2",decryptDO.getNum2())
                .eq(StringUtil.notEmpty(decryptDO.getNum2s()),"num2s",decryptDO.getNum2s())
//                .eq(StringUtil.notEmpty(decryptDO.getNum1s()),"num1s",decryptDO.getNum1s())
                .eq(StringUtil.notEmpty(decryptDO.getNum1()),"num1",decryptDO.getNum1()));
        if(decryptDO1.getStatus()==1){
            return Result.fail("加密号已绑定考生，无法修改");
        }
        if(!doList.isEmpty() ){
            //判断是不是修改自己
            if(!doList.contains(decryptDO1)){
                return Result.fail("加密号已存在");
            }
        }

        //第三步匹配 只能绑定已经录分的二次加密号
        if(StringUtil.notEmpty(decryptDO.getNum2s()) && StringUtil.notEmpty(decryptDO.getNum1s())){
            List<ExaminationResultsComDO> examinationResultsComDOS = examinationResultsComService.selectList(new EntityWrapper<ExaminationResultsComDO>().eq("field", decryptDO.getField()).eq("num2", decryptDO.getNum2s()));
            if(examinationResultsComDOS==null || examinationResultsComDOS.isEmpty()){
                return Result.fail("二次加密号'"+decryptDO.getNum2s()+"'暂未录分,请录分");
            }
        }

        //正常修改一二加密绑定
        decryptDO1.setNum1(decryptDO.getNum1());
        decryptDO1.setNum2(decryptDO.getNum2());
        decryptDO1.setNum1s(decryptDO.getNum1s());
        decryptDO1.setNum2s(decryptDO.getNum2s());
        this.updateById(decryptDO1);
        return Result.ok();

    }


    @Override
    @Transactional
    public Result updateNum1BindNum2Decrypt(DecryptDO decryptDO) {
        DecryptDO decryptDO1 = this.selectById(decryptDO.getId());
        String num2s=decryptDO1.getNum2s();
        //验证一二加密是否已经存在
        List<DecryptDO> doList = this.selectList(new EntityWrapper<DecryptDO>()
                .eq("field", decryptDO.getField())
                .eq(StringUtil.notEmpty(decryptDO.getNum2()), "num2", decryptDO.getNum2())
                //.eq(StringUtil.notEmpty(decryptDO.getNum1()),"num1",decryptDO.getNum1()))
                .eq(StringUtil.notEmpty(decryptDO.getNum2s()), "num2s", decryptDO.getNum2s())
                .eq(StringUtil.notEmpty(decryptDO.getNum1()), "num1", decryptDO.getNum1()));
        if (decryptDO1.getStatus() == 1) {
            return Result.fail("加密号已绑定考生，无法修改");
        }
        if (!doList.isEmpty()) {
            //判断是不是修改自己
            if (!doList.contains(decryptDO1)) {
                return Result.fail("加密号已存在");
            }
        }

        //只能绑定已经录分的二次加密号
        if(StringUtil.notEmpty(decryptDO.getNum2s()) && StringUtil.notEmpty(decryptDO.getNum1s())) {
            List<ExaminationResultsComDO> examinationResultsComDOS = examinationResultsComService.selectList(new EntityWrapper<ExaminationResultsComDO>().eq("field", decryptDO.getField()).eq("num2", decryptDO.getNum2s()));
            if (examinationResultsComDOS == null || examinationResultsComDOS.isEmpty()) {
                return Result.fail("二次加密号'" + decryptDO.getNum2s() + "'暂未录分,请录分");
            }
        }

        //正常修改一二加密绑定
        decryptDO1.setNum1(decryptDO.getNum1());
        decryptDO1.setNum2(decryptDO.getNum2());
        decryptDO1.setNum1s(decryptDO.getNum1s());
        decryptDO1.setNum2s(decryptDO.getNum2s());
        this.updateById(decryptDO1);

        // 查询一次加密
        DecryptDO one = this.selectOne(new EntityWrapper<DecryptDO>()
                .eq("field", decryptDO.getField())
                .eq("num1", decryptDO.getNum1s()));
        if (one != null) {
            one.setIsNum1(1);

            if(one.getNum2().equals("-")){
                //一二次加密匹配成功，以一次加密为基准改成状态为2
                one.setStatus(2);
            }

            this.updateById(one);
        }

        // 判断绑定修改的二次加密号是哪个，并修改状态
        if (decryptDO.getNum2s().equals(num2s)) {
            // 修改的二次加密号与原来绑定的二次加密号一致
            DecryptDO two = this.selectOne(new EntityWrapper<DecryptDO>().eq("field", decryptDO.getField()).eq("num2", decryptDO.getNum2s()));
            if (two != null) {
                two.setIsNum2(1);
                this.updateById(two);
            }
        }else {
            // 修改的二次加密号与原来绑定的二次加密号不一致
            // 正在更改的对象
            DecryptDO two = this.selectOne(new EntityWrapper<DecryptDO>().eq("field", decryptDO.getField()).eq("num2", decryptDO.getNum2s()));
            if (two != null) {
                two.setIsNum2(1);
                this.updateById(two);
            }
            // 更改前的对象
            // 校验是录入还是修改
            if (decryptDO.getIsScore().equals("1")){
                DecryptDO oldNum2s = this.selectOne(new EntityWrapper<DecryptDO>().eq("field", decryptDO.getField()).eq("num2", num2s));
                if (oldNum2s!=null) {
                    oldNum2s.setIsNum2(0);
                    this.updateById(oldNum2s);
                }
            }

        }

        return Result.ok();
    }

    @Override
    public Result getDecrypt(DecryptDO decryptDO) {
        Page<DecryptDO> page = new Page<DecryptDO>(decryptDO.getCurrent(), decryptDO.getSize());
        //查询已经匹配的解密列表
        Page<DecryptDO> decryptDOPage = this.selectPage(page, new EntityWrapper<DecryptDO>()
                .eq(StringUtil.notEmpty(decryptDO.getField()), "field", decryptDO.getField())
                .eq(StringUtil.notEmpty(decryptDO.getNum1s()), "num1s", decryptDO.getNum1s())
                .eq(StringUtil.notEmpty(decryptDO.getNum2s()), "num2s", decryptDO.getNum2s())
                .eq(Objects.nonNull(decryptDO.getStatus()), "status", decryptDO.getStatus())
                .ne("num1s", "-")
                .ne("num2s", "-")
                .andNew(StringUtil.notEmpty(decryptDO.getStatuss()), "status={0} or status={1}", 0, 2));

        List<DecryptDO> records = decryptDOPage.getRecords();
        if (records != null && !records.isEmpty()) {
            for (DecryptDO record : records) {
                if (record.getStatus() == 1) {
                    record.setStatuss("1");
                }
            }
            decryptDOPage.setRecords(records);
        }

        return Result.ok(decryptDOPage);
    }


    @Override
    @Transactional
    public Result getBindNum(String field, String status) {
        //查询已经匹配的解密列表
        /*List<DecryptDO> decryptDOs = this.selectList(new EntityWrapper<DecryptDO>()
                .eq(StringUtil.notEmpty(field), "field", field)
                .ne("status", 1).eq("is_num1",0).eq("is_num2",0));*/

        HashMap<String, List<DecryptDO>> map = new HashMap<>();

        // 解密第四步 根据状态判断是录入还是修改
        if ("0".equals(status)) {
            List<DecryptDO> num1 = this.baseMapper.getNum1(field);
            List<DecryptDO> num2 = this.baseMapper.getNum2(field);
            map.put("num1", num1);
            map.put("num2", num2);
        } else {
            List<DecryptDO> num1 = this.baseMapper.getNum3(field);
            List<DecryptDO> num2 = this.baseMapper.getNum4(field);
            map.put("num1", num1);
            map.put("num2", num2);
        }

        return Result.ok(map);
    }

    @Override
    public Result getAddDecrypt(DecryptDO decryptDO) {
        Page<DecryptDO> page = new Page<DecryptDO>(decryptDO.getCurrent(), decryptDO.getSize());
        //查询未匹配的解密列表
        Page<DecryptDO> decryptDOPage = this.selectPage(page, new EntityWrapper<DecryptDO>()
                .eq(StringUtil.notEmpty(decryptDO.getField()), "field", decryptDO.getField())
                .eq(StringUtil.notEmpty(decryptDO.getNum1s()), "num1s", decryptDO.getNum1s())
                .eq(StringUtil.notEmpty(decryptDO.getNum2s()), "num2s", decryptDO.getNum2s())
                //.eq("num1s","-")
                //.eq("num2s","-")
                .ne(decryptDO.getStatus() == 1 ? "num1" : "num2", "-")
                .isNotNull(StringUtil.notEmpty(decryptDO.getStatuss()), "num1"));

        //处理一次加密绑定和二次加密绑定的数据、解密完成的数据不能再编辑和删除 默认statuss=1的数据不能编辑和删除
        List<DecryptDO> records = decryptDOPage.getRecords();
        if (null != records && !records.isEmpty()) {
            for (DecryptDO record : records) {
                record.setStatuss("2");
                if (record.getStatus() == 1 || (!record.getNum1s().equals("-") && !record.getNum2s().equals("-"))) {
                    record.setStatuss("1");
                }
            }
            decryptDOPage.setRecords(records);
        }

        return Result.ok(decryptDOPage);

//        Page<DecryptDO> page = new Page<DecryptDO>(decryptDO.getCurrent(),decryptDO.getSize());
//        //查询未匹配的解密列表
//        Page<DecryptDO> decryptDOPage = this.selectPage(page,new EntityWrapper<DecryptDO>()
//                .eq(StringUtil.notEmpty(decryptDO.getField()),"field",decryptDO.getField())
//                .eq(StringUtil.notEmpty(decryptDO.getNum1s()),"num1s",decryptDO.getNum1s())
//                .eq(StringUtil.notEmpty(decryptDO.getNum2s()),"num2s",decryptDO.getNum2s())
//                .ne(decryptDO.getStatus()==1?"num1":"num2","-")
//                .isNotNull(StringUtil.notEmpty(decryptDO.getStatuss()),"num1"));
//
//        //处理一次加密绑定和二次加密绑定的数据、解密完成的数据不能再编辑和删除 默认statuss=1的数据不能编辑和删除
//        List<DecryptDO> records = decryptDOPage.getRecords();
//        if(null!=records && !records.isEmpty()){
//            for (DecryptDO record : records) {
//                record.setStatuss("2");
//                if(record.getStatus()==1 || (!record.getNum1s().equals("-")&&!record.getNum2s().equals("-"))){
//                    record.setStatuss("1");
//                }
//            }
//            decryptDOPage.setRecords(records);
//        }
//        return Result.ok(decryptDOPage);
    }

    @Override
    public void downloadImportDecrypt(HttpServletResponse response, String field, Integer type) {
        List<DecryptNum1VO> num1s = new ArrayList<>();
        List<DecryptNum2VO> num2s = new ArrayList<>();
        List<DecryptNumVO> num = new ArrayList<>();

        EntityWrapper<DecryptDO> wrapper = new EntityWrapper<DecryptDO>();
        if (type == 3) {
            wrapper.isNotNull("num1s")
                    .isNotNull("num2s").and("num1s!='-' and num2s!='-'");
        } else if (type == 2) {
            wrapper.isNotNull("num2").and("num2 !='-'");
        } else if (type == 1) {
            wrapper.isNotNull("num1").and("num1 !='-'");
        }
        //根据赛场查询已匹配赛场信息
        List<DecryptDO> decryptDO = this.selectList(wrapper
                .eq(StringUtil.notEmpty(field), "field", field)
        );
        if (decryptDO.isEmpty()) {
            return;
        }
        decryptDO.forEach(a -> {
            DecryptNum1VO decryptNum1VO = new DecryptNum1VO();
            DecryptNum2VO decryptNum2VO = new DecryptNum2VO();
            DecryptNumVO decryptNumVO = new DecryptNumVO();
            //通过反射获取相同属性值
            decryptNum1VO.setNum1(a.getNum1());
            decryptNum2VO.setNum2(a.getNum2());
            decryptNumVO.setNum1s(a.getNum1s());
            decryptNumVO.setNum2s(a.getNum2s());
            num1s.add(decryptNum1VO);
            num2s.add(decryptNum2VO);
            num.add(decryptNumVO);
        });
        switch (type) {
            case 1:
                switch (field) {
                    case "B1":
                        EasyPoiUtil.exportExcel(num1s, "婴幼儿回应性生活照护赛场一次加密信息", "婴幼儿回应性生活照护赛场一次加密信息", DecryptNum1VO.class, "婴幼儿回应性生活照护赛场一次加密信息", response);
                        break;
                    case "B2":
                        EasyPoiUtil.exportExcel(num1s, "婴幼儿卫生保健与疾病预防赛场一次加密信息", "婴幼儿卫生保健与疾病预防赛场一次加密信息", DecryptNum1VO.class, "婴幼儿卫生保健与疾病预防赛场一次加密信息", response);
                        break;
                    case "B3":
                        EasyPoiUtil.exportExcel(num1s, "婴幼儿意外伤害的应急处置赛场一次加密信息", "婴幼儿意外伤害的应急处置赛场一次加密信息", DecryptNum1VO.class, "婴幼儿意外伤害的应急处置赛场一次加密信息", response);
                        break;
                    case "B4":
                        EasyPoiUtil.exportExcel(num1s, "婴幼儿早期发展赛场一次加密信息", "婴幼儿早期发展赛场一次加密信息", DecryptNum1VO.class, "婴幼儿早期发展赛场一次加密信息", response);
                        break;
                    case "C":
                        EasyPoiUtil.exportExcel(num1s, "婴幼儿健康养育照护知识测评一次加密信息", "婴幼儿健康养育照护知识测评一次加密信息", DecryptNum1VO.class, "婴幼儿健康养育照护知识测评一次加密信息", response);
                        break;
                }
                break;
            case 2:
                switch (field) {
                    case "B1":
                        EasyPoiUtil.exportExcel(num2s, "婴幼儿回应性生活照护赛场二次加密信息", "婴幼儿回应性生活照护赛场二次加密信息", DecryptNum2VO.class, "婴幼儿回应性生活照护赛场二次加密信息", response);
                        break;
                    case "B2":
                        EasyPoiUtil.exportExcel(num2s, "婴幼儿卫生保健与疾病预防赛场二次加密信息", "婴幼儿卫生保健与疾病预防二次加密信息", DecryptNum2VO.class, "婴幼儿卫生保健与疾病预防二次加密信息", response);
                        break;
                    case "B3":
                        EasyPoiUtil.exportExcel(num2s, "婴幼儿意外伤害的应急处置赛场二次加密信息", "婴幼儿意外伤害的应急处置赛场二次加密信息", DecryptNum2VO.class, "婴幼儿意外伤害的应急处置赛场二次加密信息", response);
                        break;
                    case "B4":
                        EasyPoiUtil.exportExcel(num2s, "婴幼儿早期发展赛场二次加密信息", "婴幼儿早期发展赛场二次加密信息", DecryptNum2VO.class, "婴幼儿早期发展赛场二次加密信息", response);
                        break;
                    case "C":
                        EasyPoiUtil.exportExcel(num2s, "婴幼儿健康养育照护知识测评二次加密信息", "婴幼儿健康养育照护知识测评二次加密信息", DecryptNum2VO.class, "婴幼儿健康养育照护知识测评二次加密信息", response);
                        break;
                }
                break;
            case 3:
                switch (field) {
                    case "B1":
                        EasyPoiUtil.exportExcel(num, "婴幼儿回应性生活照护一次加密与二次加密匹配信息", "婴幼儿回应性生活照护一次加密与二次加密匹配信息", DecryptNumVO.class, "婴幼儿回应性生活照护一次加密与二次加密匹配信息", response);
                        break;
                    case "B2":
                        EasyPoiUtil.exportExcel(num, "婴幼儿卫生保健与疾病预防赛场一次加密与二次加密匹配信息", "婴幼儿卫生保健与疾病预防一次加密与二次加密匹配信息", DecryptNumVO.class, "婴幼儿卫生保健与疾病预防一次加密与二次加密匹配信息", response);
                        break;
                    case "B3":
                        EasyPoiUtil.exportExcel(num, "婴幼儿意外伤害的应急处置赛场一次加密与二次加密匹配信息", "婴幼儿意外伤害的应急处置赛场一次加密与二次加密匹配信息", DecryptNumVO.class, "婴幼儿意外伤害的应急处置赛场一次加密与二次加密匹配信息", response);
                        break;
                    case "B4":
                        EasyPoiUtil.exportExcel(num, "婴幼儿早期发展赛场一次加密与二次加密匹配信息", "婴幼儿早期发展赛场一次加密与二次加密匹配信息", DecryptNumVO.class, "婴幼儿早期发展赛场一次加密与二次加密匹配信息", response);
                        break;
                    case "C":
                        EasyPoiUtil.exportExcel(num, "婴幼儿健康养育照护知识测评一次加密与二次加密匹配信息", "婴幼儿健康养育照护知识测评一次加密与二次加密匹配信息", DecryptNumVO.class, "婴幼儿健康养育照护知识测评一次加密与二次加密匹配信息", response);
                        break;

                }
                break;
            default:
                return;
        }
    }


    @Override
    public Result deltedDecrypt(Long id, String status) {
        DecryptDO decryptDO = this.selectById(id);
        if (!decryptDO.getNum1s().equals("-") && !decryptDO.getNum2s().equals("-")) {
            return Result.fail("一次加密和二次加密已经关联，无法刪除");
        }
        if (decryptDO.getStatus() == 1) {
            return Result.fail("加密号已绑定考生，无法刪除");
        }
        if (ObjectUtils.isEmpty(decryptDO)) {
            return Result.fail("刪除失败");
        }
        decryptDO.setNum1s(null);
        decryptDO.setNum2s(null);
        if (status.equals("1")) {
            decryptDO.setNum1("-");
        } else if (status.equals("2")) {
            //录的分数也需要删除
            examinationResultsComService.delete(new EntityWrapper<ExaminationResultsComDO>().eq("num2", decryptDO.getNum2()).eq("field", decryptDO.getField()));
            decryptDO.setNum2("-");
        }
        return Result.ok(this.updateById(decryptDO));
    }

    @Override
    @Transactional
    public Result updateStuCopy(Long id, String num1s, String field) {
        //根据一次加密跟赛场查询二次加密
        DecryptDO decryptDO = this.selectOne(new EntityWrapper<DecryptDO>()
                .eq("num1s", num1s).eq("field", field));
        if (Objects.isNull(decryptDO)) {
            return Result.fail("没有绑定的二次加密号");
        }
        //二次加密+赛场查询成绩
        ExaminationResultsComDO resultsComDO = examinationResultsComService.selectOne(new EntityWrapper<ExaminationResultsComDO>()
                .eq("num2", decryptDO.getNum2s()).eq("field", field));
        if (Objects.isNull(resultsComDO)) {
            return Result.fail("二次加密号没有分数");
        }
        //修改加密状态为已绑定
        decryptDO.setStatus(1);
        //如果是修改把之前的加密号状态改为2
        //根据考生+赛场查询绑定的一二次加密
        EncipherDO encipherDO = Optional.ofNullable(
                encipherService.selectOne(new EntityWrapper<EncipherDO>()
                        .eq("stu_id", id).eq("field", field))).orElse(new EncipherDO());
        encipherDO.setStuId(id);
        encipherDO.setNum1(decryptDO.getNum1s());
        encipherDO.setNum2(decryptDO.getNum2s());
        encipherDO.setField(field);
        if (Objects.nonNull(encipherDO.getId())) {
            //绑定修改考生模块加密信息
            encipherService.updateById(encipherDO);
            decryptDO.setStatus(2);
        } else {
            //绑定新增考生模块加密信息
            encipherService.insert(encipherDO);
        }
        this.updateById(decryptDO);
        resultsComDO.setStuSignId(id);
        examinationResultsComService.updateById(resultsComDO);

        //选手设置为解密
        CareStuSignUpComRealityDO careStuSignUpComRealityDO = new CareStuSignUpComRealityDO();
        careStuSignUpComRealityDO.setId(id);
        careStuSignUpComRealityDO.setDecrypt(1);
        careStuSignUpComRealityService.updateById(careStuSignUpComRealityDO);

        return Result.ok();
    }

    @Override
    @Transactional
    public Result updateEncipher(Long id, String num1s, String field, boolean oneself) {
        //判断是否是修改自己原本的加密号 ： A01->A02,A02->A01
        if (!oneself) {
            EncipherDO encipherDOS = encipherService.selectOne(new EntityWrapper<EncipherDO>().eq("num1", num1s).eq("field", field));
            if (Objects.nonNull(encipherDOS) && Objects.equals(id, encipherDOS.getStuId())) {
                return Result.ok();
            }
            if (Objects.nonNull(encipherDOS)) {
                return Result.fail("修改的一次加密号已被使用");
            }
        }
        //根据一次加密跟赛场查询二次加密
        DecryptDO decryptDO = this.selectOne(new EntityWrapper<DecryptDO>().eq("num1s", num1s).eq("field", field));
        //把已使用的加密号状态改成1
        /*decryptDO.setStatus(1);
        this.updateById(decryptDO);*/

        //根据考生id+赛场修改加密信息
        EncipherDO encipherDO = encipherService.selectOne(new EntityWrapper<EncipherDO>().eq("field",field).eq("stu_id",id));


        /*if (!oneself) {
            //把不使用的加密号状态改成0
            EncipherDO encipherDO = encipherService.selectOne(new EntityWrapper<EncipherDO>().eq("field", field).eq("stu_id", id));
            if (field.equals("A") || field.equals("C")) {
                StuSignUpComDO stuSignUpComDO = stuSignUpComService.selectOne(new EntityWrapper<StuSignUpComDO>()
                        .eq("num2", encipherDO.getNum2())
                        .eq("competition_type", field.equals("A") ? 1 : 2));
                stuSignUpComDO.setStatus(0);
                stuSignUpComService.updateById(stuSignUpComDO);
            } else {
                DecryptDO decryptDO1 = this.selectOne(new EntityWrapper<DecryptDO>()
                        .eq("num1s", encipherDO.getNum1())
                        .eq("field", encipherDO.getField()));
                decryptDO1.setStatus(2);
               this.updateById(decryptDO1);
            }
            //根据考生id+赛场修改加密信息
            encipherDO.setNum1(decryptDO.getNum1s());
            //encipherDO.setNum2(decryptDO.getNum2s());
            encipherService.update(encipherDO, new EntityWrapper<EncipherDO>().eq("stu_id", id).eq("field", field));
        }*/

        //把不使用的加密号状态改成0
        if(field.equals("A") || field.equals("C")){
            if(!oneself){
                StuSignUpComDO stuSignUpComDO = stuSignUpComService.selectOne(new EntityWrapper<StuSignUpComDO>()
                        .eq("num2", encipherDO.getNum2())
                        .eq("competition_type", field.equals("A") ? 1 : 2));
                stuSignUpComDO.setStatus(0);
                stuSignUpComService.updateById(stuSignUpComDO);
            }
            //还原之前的状态为 已录分
            DecryptDO decryptDO1 = new DecryptDO();
            decryptDO1.setStatus(2);
            this.update(decryptDO1,new EntityWrapper<DecryptDO>().eq("field",field).eq("num2s",encipherDO.getNum2()));

            encipherDO.setNum1(num1s);
            encipherDO.setNum2(decryptDO.getNum2s());
            encipherService.update(encipherDO,new EntityWrapper<EncipherDO>().eq("stu_id",id).eq("field",field));

        }else{
            //通过选手id查询院校
            CareStuSignUpComRealityDO careStuSignUpComRealityDO = careStuSignUpComRealityService.selectById(id);
            //使用重置接口修改上一条数据的状态
            resetDecrypt(field,careStuSignUpComRealityDO.getUnitName());
            //重新新增
            EncipherDO encipherDO1 = new EncipherDO();
            encipherDO1.setNum1(num1s);
            encipherDO1.setNum2(decryptDO.getNum2s());
            encipherDO1.setStuId(id);
            encipherDO1.setField(field);
            encipherService.insert(encipherDO1);
            //修改选手绑定成绩信息
            ExaminationResultsComDO examinationResultsComDO = new ExaminationResultsComDO();
            examinationResultsComDO.setStuSignId(id);
            examinationResultsComService.update(examinationResultsComDO,new EntityWrapper<ExaminationResultsComDO>().eq("field",field).eq("num2",decryptDO.getNum2s()));

        }

        //根据一次加密跟赛场查询二次加密
        //把已使用的加密号状态改成1
        decryptDO.setStatus(1);
        this.updateById(decryptDO);

        //选手设置为解密
        CareStuSignUpComRealityDO careStuSignUpComRealityDO = new CareStuSignUpComRealityDO();
        careStuSignUpComRealityDO.setId(id);
        careStuSignUpComRealityDO.setDecrypt(1);
        careStuSignUpComRealityService.updateById(careStuSignUpComRealityDO);
        return Result.ok();
    }

    /**
     * 一键解密
     *
     * @return
     */
    @Override
    public Result updateStuDecrypt(String username, String password, String username2, String password2) {
        //通过裁判长和仲裁长账号密码核对 匹配上才能解密
        boolean flag = false;
        //查询角色"裁判长" 下的账号 一一匹配
        List<UserDO> userDOList = userDao.getUserByRole(refereeName);
        if (null != userDOList && !userDOList.isEmpty()) {
            for (UserDO userDO : userDOList) {
                if (Objects.equals(PasswdUtils.get(password, userDO.getSalt()), userDO.getPassword())) {
                    if (username.equals(userDO.getUsername())) {
                        flag = true;
                        break;
                    }
                }
            }
        }

        boolean flag2 = false;
        //查询角色"仲裁长" 下的账号 一一匹配
        List<UserDO> userByRoleList = userDao.getUserByRole(arbitrationName);
        if (null != userByRoleList && !userByRoleList.isEmpty()) {
            for (UserDO userDO : userByRoleList) {
                if (Objects.equals(PasswdUtils.get(password2, userDO.getSalt()), userDO.getPassword())) {
                    if (username2.equals(userDO.getUsername())) {
                        flag2 = true;
                        break;
                    }
                }
            }
        }

        if (!flag) {
            return Result.fail("裁判长账号或密码不正确");
        } else if (!flag2) {
            return Result.fail("仲裁长账号或密码不正确");
        }

        CareStuSignUpComRealityDO comRealityDO = new CareStuSignUpComRealityDO();
        comRealityDO.setDecryptAll(1);
        careStuSignUpComRealityService.update(comRealityDO, new EntityWrapper<CareStuSignUpComRealityDO>().eq("decrypt_all", 0));
        return Result.ok();
    }

    /**
     * 查询没有录分的二次加密号
     *
     * @return
     */
    @Override
    public Result getNum2Init(DecryptDO decryptDO) {
        //存在两种情况 1.理论制定照护计划和素养测评 需要查询虚拟的二次加密 2.实操是查询录分时导入的二次加密号
        if (decryptDO.getField().equals("C")) {
            Page<StuSignUpComDO> stuSignUpComDOPage = new Page<>(decryptDO.getCurrent(), decryptDO.getSize());
            Page<StuSignUpComDO> stuSignUpComDOPage1 = stuSignUpComService.selectPage(stuSignUpComDOPage, new EntityWrapper<StuSignUpComDO>()
                    .eq("status", 0)
                    .isNotNull("num2")
                    .eq("competition_type", 2));
            return Result.ok(stuSignUpComDOPage1);
        } else {
            Page<DecryptDO> decryptDOPage = new Page<>(decryptDO.getCurrent(), decryptDO.getSize());
            Page<DecryptDO> decryptDOPage1 = this.selectPage(decryptDOPage, new EntityWrapper<DecryptDO>()
                    .eq("field", decryptDO.getField())
                    .isNotNull("num2")
                    .eq("status", 0));
            return Result.ok(decryptDOPage1);
        }

    }


    /**
     * 录分绑定重置
     *
     * @return
     */
    @Override
    @Transactional
    public Result<?> resetDecrypt(String field, String unitName) {
        //通过参赛单位查询选手信息
        List<CareStuSignUpComRealityDO> careStuSignUpComRealityDOS = careStuSignUpComRealityService.selectList(new EntityWrapper<CareStuSignUpComRealityDO>().eq("unit_name", unitName));
        if (null != careStuSignUpComRealityDOS && !careStuSignUpComRealityDOS.isEmpty()) {
            for (CareStuSignUpComRealityDO careStuSignUpComRealityDO : careStuSignUpComRealityDOS) {
                //查询查赛选手的一、二次加密号
                EncipherDO encipherDO = encipherService.selectOne(new EntityWrapper<EncipherDO>().eq("stu_id", careStuSignUpComRealityDO.getId()).eq("field", field));
                if (null != encipherDO) {
                    //先修改选手的状态改为已录分
                    DecryptDO decryptDO = new DecryptDO();
                    decryptDO.setStatus(2);
                    this.update(decryptDO, new EntityWrapper<DecryptDO>().eq("field", field).eq("num1s", encipherDO.getNum1()).eq("num2s", encipherDO.getNum2()));
                    //再删除绑定记录
                    encipherService.deleteById(encipherDO.getId());
                }
                //清空成绩表的选手id
                examinationResultsDao.updateStuSignId(careStuSignUpComRealityDO.getId(),field);
            }
        }

        return Result.ok();
    }

    /**
     * 一次二次加密绑定重置  赛场
     *
     * @return
     */
    @Override
    @Transactional
    public Result<?> resetNum1BindNum2Decrypt(String field,Long id) {
        // 根据id查询已绑定的号码
        DecryptDO decryptDO = this.selectById(id);
        String num2= decryptDO.getNum2s();
        if (decryptDO == null) {
            return Result.fail("重置失败");
        }
        // 将已绑定的一次二次加密号清除
        decryptDO.setNum1s("-");
        decryptDO.setNum2s("-");
        // 将一次加密号设置为未使用
        decryptDO.setIsNum1(0);
        this.updateById(decryptDO);

        // 根据绑定的二次加密号查询二次加密号的信息
        DecryptDO decryptDO2 = this.selectOne(new EntityWrapper<DecryptDO>().eq("num2", num2)
                .eq("field", field));

        if (decryptDO2!=null){
            // 将二次加密号设置为未使用
            decryptDO2.setIsNum2(0);
            // 更新数据
            this.updateById(decryptDO2);
        }

        return Result.ok();
    }

}
